<template>
  <div>
    <div class="table_card">
      <div class="table_card_top">
        <span class="solid_left"></span>
        <div class="table_card_title">客户来源</div>
        <div class="op_right">
          <button class="primary_button" @click="NewSource">新增来源</button>
        </div>
      </div>

      <div class="table_list">
        <el-table :data="sourcetList" stripe style="width: 100%">
          <el-table-column prop="name" label="来源名称" width="300px">
          </el-table-column>
          <el-table-column prop="remark" label="来源备注"></el-table-column>
          <el-table-column label="操作" width="200px">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="EditSource(scope.row)"
                >编辑</el-button
              >
              <span class="separate_right">|</span>
              <el-button
                size="mini"
                type="text"
                @click="DelSource(scope.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>

      <!-- 新增来源 -->
      <el-dialog
        :visible.sync="sourcedialogVisible"
        width="400px"
        :title="title"
        class="no-dialog-padding"
        @close="CancelSourceSubmit"
      >
        <span>
          <div class="source_box">
            <el-form ref="SourceForm" :rules="SourceRules" :model="SourceForm">
              <el-row>
                <el-col>
                  <el-form-item label="来源名称" prop="name">
                    <el-input
                      class="w-100"
                      v-model="SourceForm.name"
                      placeholder="请输入来源名称"
                    ></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-form-item label="来源备注" prop="remark">
                  <el-input
                    type="textarea"
                    placeholder="请输入来源备注"
                    v-model="SourceForm.remark"
                    maxlength="30"
                    :rows="4"
                    show-word-limit
                  />
                </el-form-item>
              </el-row>
            </el-form>
          </div>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button class="cancel_btn" @click="CancelSourceSubmit"
            >取 消</el-button
          >
          <el-button
            type="primary"
            @click="SourceSubmit"
            :loading="CreateClienLoading"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
  </div>
</template>
    
    <script>
import {
  getClientSource,
  CreateClientSource,
  UpdateClientSource,
  DeleteClientSource,
} from "@/api/business/client";

export default {
  name: "EryuoaStoreFrontendClientlist",

  data() {
    return {
      sourcedialogVisible: false, // 新增来源 编辑来源
      loading: false,
      sourcetList: [], // 来源列表
      title: "",

      CreateClienLoading: false,
      // 新建来源
      SourceForm: {
        id: "",
        name: "",
        remark: "",
      },

      SourceRules: {
        name: [{ required: true, message: "请输入来源名称", trigger: "blur" }],
      },

      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
      },

      total: 0,
    };
  },

  created() {
    this.getList();
  },

  mounted() {},

  methods: {
    // 来源列表
    getList() {
      this.loading = true;
      getClientSource(this.queryParams)
        .then((res) => {
          console.log(res);
          this.loading = false;
          const { code, data } = res;
          if (code === 0) {
            this.sourcetList = data.list;
            this.total = data.total;
          }
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    CancelSourceSubmit() {
      this.SourceForm.id = "";
      this.$refs["SourceForm"].resetFields();
      this.SourceForm = {
        id: "",
        name: "",
        remark: "",
      };

      this.CreateClienLoading = false;
      this.$nextTick(() => {
        this.sourcedialogVisible = false;
      });
    },

    SourceSubmit() {
      this.$refs["SourceForm"].validate((valid) => {
        if (valid) {
          if (this.CreateClienLoading) {
            return false;
          }

          if (!!this.SourceForm.id) {
            this.UpdateSource();
          } else {
            this.NewAddSource();
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    // 新建来源
    NewSource() {
      this.sourcedialogVisible = true;
      this.title = "新增来源";
      // 客户来源数据
    },

    // 新建来源 按钮
    NewAddSource() {
      this.CreateClienLoading = true;
      CreateClientSource(this.SourceForm)
        .then((res) => {
          this.CreateClienLoading = false;
          const { code, data } = res;
          if (code === 0) {
            this.getList();
            this.CancelSourceSubmit();
            this.$modal.msgSuccess("操作成功");
          }
        })
        .catch((err) => {
          this.CreateClienLoading = false;
        });
    },

    // 编辑来源 按钮
    UpdateSource() {
      let SourceForm = JSON.parse(JSON.stringify(this.SourceForm));
      const { id, name, remark } = SourceForm;
      this.CreateClienLoading = true;
      UpdateClientSource({ id: id, name: name, remark: remark })
        .then((res) => {
          this.CreateClienLoading = false;
          const { code, data } = res;
          if (code === 0) {
            this.getList();
            this.CancelSourceSubmit();
            this.$modal.msgSuccess("操作成功");
          }
        })
        .catch((err) => {
          this.CreateClienLoading = false;
        });
    },

    // 编辑来源
    EditSource(row) {
      this.sourcedialogVisible = true;
      this.title = "编辑来源";
      this.SourceForm = JSON.parse(JSON.stringify(row));
    },

    // 删除来源
    DelSource(id) {
      this.$confirm("来源删除后将无法撤回, 是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          DeleteClientSource({ id: id })
            .then((res) => {
              const { code, data } = res;
              if (code === 0) {
                this.getList();
                this.$modal.msgSuccess("操作成功");
              }
            })
            .catch(() => {});
        })
        .catch(() => {});
    },
  },
};
</script>
    
    <style lang="scss" scoped>
.w-200 {
  width: 200px;
  background: #ffffff;
  border-radius: 4px;
}

.client_name {
  font-size: 14px;
  font-weight: 400;
  color: #0052d9;
}

.give {
  height: 40px;
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
  margin-top: 40px;
  line-height: 40px;
}

.el-form-item {
  margin-bottom: 20px;
}

.recharge_form {
  padding: 10px 24px;
}

.memberName {
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
}
.el_row_item {
  margin-bottom: 10px;
}
.el_row_date {
  margin-top: 20px;
}
.el_row_reason {
  margin-top: 10px;
}

.el_row_date {
  .el-form-item {
    margin-bottom: 0px;
  }
}
.separate_right {
  width: 1px;
  height: 14px;
  //   background: #e9e9e9;
  margin-left: 12px;
  margin-right: 12px;
}

.source_box {
  margin: 0 24px;
}
</style>